package com.xxs.directive;

import java.io.Writer;
import java.util.List;

import org.dom4j.Element;

import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.stat.Scope;
import com.xxs.common.Const;
import com.xxs.utils.xml.XmlDom4jUtils;

public class NodeListDirective extends Directive {

	@Override
	public void setExprList(ExprList exprList) {
		super.setExprList(exprList);
	}

	@Override
	public void exec(Env env, Scope scope, Writer writer) {
		String siteCode = exprList.eval(scope).toString();
		List<Element> elements = XmlDom4jUtils.getElements(getXmlPath(siteCode, "node"),"node");
		scope.set("elements", elements);
	}
	
	public String getXmlPath(String siteCode,String modelName){
		if(siteCode!=null){
			return Const.XML_PREFIX+siteCode+"/"+modelName+Const.XML_SUFFIX;
		}else{
			return Const.XML_PREFIX+modelName+Const.XML_SUFFIX;
		}
	}
}
